package com.wc.AlgoOJ_train.train3_算法大礼包.c3_贪心.AG0406_站岗;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Comparator;
import java.util.StringTokenizer;

/**
 * @Author congge
 * @Date 2024/1/29 20:43
 * @description http://43.138.190.70:8888/p/AG0406
 */
public class Main {
    /**
     * 这数据 t 有 1000000
     * dp, 线段树dp, minv就是最小值
     */
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static int N = 25010, M = 1000010;
    static int[][] a = new int[N][2];
    static int[] minv = new int[M << 2], maxv = new int[M << 2], laz = new int[M << 2];
    static int n, t;

    public static void main(String[] args) {
        n = sc.nextInt();
        t = sc.nextInt();
        for (int i = 1; i <= n; i++) {
            a[i][0] = sc.nextInt();
            a[i][1] = sc.nextInt();
        }
        // 按照最先的时间的排列
        Arrays.sort(a, 1, n + 1, Comparator.comparingInt(o -> o[0]));
        build(1, 1, t, N);
        for (int i = 1; i <= n; i++) {
            int x = 0;
            if (a[i][0] > 1) x = get(1, 1, t, a[i][0] - 1);
            modify(1, 1, t, a[i][0], a[i][1], x + 1);
        }
        if (get(1, 1, t, 1) != 1 || get(1, 1, t, t) == N) out.println(-1);
        else out.println(get(1, 1, t, t));
        out.flush();
    }

    static void modify(int u, int l, int r, int ql, int qr, int x) {
        if (ql == l && qr == r) {
            if (maxv[u] <= x) return;
            if (minv[u] > x) {
                minv[u] = maxv[u] = x;
                laz[u] = x;
                return;
            }
            pushDown(u);
            int mid = l + r >> 1;
            modify(u << 1, l, mid, ql, mid, x);
            modify(u << 1 | 1, mid + 1, r, mid + 1, qr, x);
            pushUp(u);
            return;
        }
        if (maxv[u] <= x) return;
        pushDown(u);
        int mid = l + r >> 1;
        if (qr <= mid) modify(u << 1, l, mid, ql, qr, x);
        else if (ql > mid) modify(u << 1 | 1, mid + 1, r, ql, qr, x);
        else {
            modify(u << 1, l, mid, ql, mid, x);
            modify(u << 1 | 1, mid + 1, r, mid + 1, qr, x);
        }
        pushUp(u);
    }

    static int get(int u, int l, int r, int k) {
        if (l == r) return minv[u];
        pushDown(u);
        int mid = l + r >> 1;
        if (k <= mid) return get(u << 1, l, mid, k);
        return get(u << 1 | 1, mid + 1, r, k);
    }

    static void build(int u, int l, int r, int v) {
        if (l == r) {
            minv[u] = maxv[u] = v;
            return;
        }
        int mid = l + r >> 1;
        build(u << 1, l, mid, v);
        build(u << 1 | 1, mid + 1, r, v);
        pushUp(u);
    }


    static void pushUp(int u) {
        minv[u] = Math.min(minv[u << 1], minv[u << 1 | 1]);
        maxv[u] = Math.max(maxv[u << 1], maxv[u << 1 | 1]);
    }

    static void pushDown(int u) {
        if (laz[u] != 0) {
            laz[u << 1] = laz[u << 1 | 1] = laz[u];
            minv[u << 1] = minv[u << 1 | 1] = laz[u];
            maxv[u << 1] = maxv[u << 1 | 1] = laz[u];
            laz[u] = 0;
        }
    }
}

class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    // 是否由下一个
    boolean hasNext() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                String line = br.readLine();
                if (line == null)
                    return false;
                st = new StringTokenizer(line);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return true;
    }
}
